package com.example.tsproject.Controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.tsproject.Utils.ThreadLocalUtil;
import com.example.tsproject.entity.*;
import com.example.tsproject.service.PostsService;
import com.example.tsproject.service.TeachersService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class PostsController {
    @Autowired
    private PostsService postsService;
    @Autowired
    private TeachersService teachers;
    // 分页查询
    @GetMapping("a")
    public Result list(
            Integer pageNum,
            Integer pageSize){
//        System.out.println(pageSize);
//        System.out.println(pageNum);
        Page<Posts> pageInfo = new Page<>(pageNum,pageSize);
        pageInfo = postsService.page(pageInfo);
        List<Posts> records = pageInfo.getRecords();
        return Result.success(records);
    }

    // Add a new post
    @PostMapping("/posts")
    public Result addPost(@RequestParam("Content") String Content) {
        Posts post = new Posts();
        post.setContent(Content);
        String employee_number= ThreadLocalUtil.get();
        System.out.println(employee_number);
        Teachers teacher = teachers.findByEmployeeNumber(employee_number);
        post.setTeacher_id(1);
        post.setContent(Content);
        post.setPost_time(new Date());
        boolean isSaved = postsService.save(post);
        if (isSaved) {
            return Result.success("Post added successfully");
        } else {
            return Result.error("Failed to add post");
        }
    }

    // Delete a post by ID
    @DeleteMapping("/posts/{id}")
    public Result deletePost(@PathVariable Integer id) {
        boolean isRemoved = postsService.removeById(id);
        if (isRemoved) {
            return Result.success("Post deleted successfully");
        } else {
            return Result.error("Failed to delete post");
        }
    }

    // Find a post by ID
    @GetMapping("/posts/{id}")
    public Result findPostById(@PathVariable Integer id) {
        Posts post = postsService.getById(id);
        if (post != null) {
            return Result.success(post);
        } else {
            return Result.error("Post not found");
        }
    }
    @GetMapping("/postsfindAll")
    public Result findAllPost() {
        ArrayList<Posts> posts = (ArrayList<Posts>) postsService.list();
        if (posts != null) {
            return Result.success(posts);
        } else {
            return Result.error("Post not found");
        }
    }
    // Multi-table query to get posts with teacher details
    @GetMapping("/postsWithTeachers")
    public Result getPostsWithTeachers() {
        List<PostsWithTeacher> postsWithTeachers = postsService.getPostsWithTeachers();
//        System.out.println(postsWithTeachers.get(10));
        if (postsWithTeachers != null) {
            return Result.success(postsWithTeachers);
        } else {
            return Result.error("No posts found");
        }
    }
}
